package net.dopan.pigframe.demo.service.impl;

import net.dopan.pigframe.config.utils.LocationUtils;
import net.dopan.pigframe.core.base.service.impl.BaseServiceImpl;
import net.dopan.pigframe.demo.mapper.UserActivityMapper;
import net.dopan.pigframe.demo.model.entity.CommentLike;
import net.dopan.pigframe.demo.model.entity.UserActivity;
import net.dopan.pigframe.demo.model.entity.vo.*;
import net.dopan.pigframe.demo.service.UserActivityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(rollbackFor = RuntimeException.class)

public class UserActivityServiceImpl extends BaseServiceImpl<UserActivityMapper, UserActivity> implements UserActivityService {

    @Autowired
    private UserActivityMapper userActivityMapper;

    @Override
    public Boolean addUserActivity(UserActivity userActivity, boolean flag) {

        //根据经纬度获取省市区
        if (StringUtils.isNotBlank(userActivity.getLongitude()) & StringUtils.isNotBlank(userActivity.getLatitude())) {
            try {
                String[] arr = LocationUtils.getAdd(userActivity.getLongitude(), userActivity.getLatitude());
                userActivity.setProvince(arr[0]);
                userActivity.setCity(arr[1]);
                userActivity.setArea(arr[2]);
            } catch (IndexOutOfBoundsException e) {
                System.err.println("经纬度格式不正确");
                e.printStackTrace();
            }

        }
        userActivity.setCollectNum(0);
        userActivity.setLikeNum(0);
        userActivity.setUsername(userActivityMapper.findUsernameByUserId((int) userActivity.getUserId()));
        Date date = new Date();
        userActivity.setCreateTime(date);
        if (flag) {
            /** 发起活动为true 编辑活动为false */
            userActivity.setCommentNum(0);
            /** 1为活动 2 为笔记 */
            userActivity.setTypeFlag(1);
            userActivityMapper.insert(userActivity);
            /** 添加用户发布活动动态*/
            UserEventVo userEventVo = new UserEventVo();
            userEventVo.setCreateTime(date);
            userEventVo.setEventMsgId(userActivity.getActivityId());
            userEventVo.setEventTypeId(1);
            userEventVo.setUserId(userActivity.getUserId());
            userActivityMapper.addUserEvent(userEventVo);

        } else {
            userActivityMapper.updateUserActivity(userActivity);
        }

        return true;
    }

    @Override
    public UserActivity getUserActivity(Integer activityId) {
        return userActivityMapper.selectById(activityId);
    }

    @Override
    public Integer findMaxId() {
        return userActivityMapper.findMaxId();
    }

    @Override
    public Integer findEntryActivityNum(Integer activityId) {
        return userActivityMapper.findEntryActivityNum(activityId);
    }

    @Override
    public List<UserActivityVo> findUserActivityVo(Map<String, String> map) {


        List<UserActivityVo> userActivityVoList = new ArrayList<>();
        //时间
        if (StringUtils.isNotBlank(map.get("timeSelection"))) {
            //周末
            if (map.get("timeSelection").equals("weekend")) {
                map.put("weekend", "weekend");
                map.put("day", "30");
            }
            //一周内
            if (map.get("timeSelection").equals("a_week")) {
                map.put("day", "7");
            }
            //一个月内
            if (map.get("timeSelection").equals("a_month")) {
                map.put("day", "30");
            }
        } else {
            //默认提供一个月内热门活动
            map.put("day", "30");
        }

        userActivityVoList = userActivityMapper.findUserActivityVo(map);
        for (UserActivityVo userActivityVo : userActivityVoList
        ) {

            if (StringUtils.isNotBlank(map.get("lat2")) && StringUtils.isNotBlank(map.get("lng2"))) {
                Double lat1 = Double.parseDouble(userActivityVo.getLatitude());
                Double lng1 = Double.parseDouble(userActivityVo.getLongitude());
                //获取用户的位置信息
                Double lat2 = Double.parseDouble(map.get("lat2"));
                Double lng2 = Double.parseDouble(map.get("lng2"));
                //用户与活动之间的距离
                userActivityVo.setDistance(LocationUtils.getDistance(lat1, lng1, lat2, lng2));
            }
//            //活动参与人数
//            userActivityVo.setEntryActivityNum(userActivityMapper.findEntryActivityNum((int) userActivityVo.getActivityId()));
        }
        //类型
        if (StringUtils.isNotBlank(map.get("type"))) {
            //按时间排序
            if (map.get("type").equals("0")) {
                return userActivityVoList;
            }
            //按距离排序
            if (map.get("type").equals("1")) {
                Collections.sort(userActivityVoList, new Comparator<UserActivityVo>() {

                    @Override
                    public int compare(UserActivityVo v1, UserActivityVo v2) {
                        //按照距离进行升序排列
                        if (v1.getDistance() > v2.getDistance()) {
                            return 1;
                        }
                        if (v1.getDistance() < v2.getDistance()) {
                            return -1;
                        }
                        return 0;
                    }
                });
            }
        }

        return userActivityVoList;
    }

    @Override
    public List<UserActivityVo> getActivityDetail(String activityId, String userId) {
        return userActivityMapper.getActivityDetail(activityId, userId);
    }

    @Override
    public List<Map<String, String>> getNoteDetail(String noteId, String userId) {
        return userActivityMapper.getNoteDetail(noteId, userId);
    }

    @Override
    public List<Map<String, String>> getPhotoWall(String activityId, String userId) {
        return userActivityMapper.getPhotoWall(activityId, userId);
    }

    @Override
    public Boolean addPhotoIntoWall(Integer activityId, String photoUrl, String userId) {
        userActivityMapper.addPhotoIntoWall(activityId, photoUrl, userId);
        return true;
//        return userActivityMapper.getPhotoWall(String.valueOf(activityId), userId);
    }

    @Override
    public Boolean addActivityComment(ActivityCommentVo activityCommentVo) {
        /** 更新评论数 */
        long num = activityCommentVo.getCommentNum();
        userActivityMapper.updateCommentNum(String.valueOf(activityCommentVo.getActivityId()), String.valueOf(num));
        userActivityMapper.addActivityComment(activityCommentVo);
        return true;
    }

    @Override
    public Boolean addUserNoteComment(String noteId, String userId, String commentMsg, String commentNum) {
        /** 更新评论数 */
        userActivityMapper.updateNoteCommentNum(noteId, commentNum);
        userActivityMapper.addNoteComment(noteId, userId, commentMsg);
        return true;
    }


    @Override
    public Integer getCommentNum(String activityId) {
        return userActivityMapper.getCommentNum(activityId);
    }

    @Override
    public Integer getNoteCommentNum(String noteId) {
        return userActivityMapper.getNoteCommentNum(noteId);
    }

    @Override
    public Boolean addCommentReply(CommentReplyBo commentReplyBo) {
        userActivityMapper.addCommentReply(commentReplyBo);
        return true;
    }

    @Override
    public Boolean addNoteCommentReply(String userId, String commentId, String replyMsg) {
        userActivityMapper.addNoteCommentReply(userId, commentId, replyMsg);
        return true;
    }

    @Override
    public CommentLike findCommentLik(String commentId, String userId) {
        return userActivityMapper.findCommentLike(commentId, userId);
    }

    @Override
    public List<Map<String, String>> findNoteCommentLike(String commentId, String userId) {
        return userActivityMapper.findNoteCommentLike(commentId, userId);
    }

    @Override
    public Integer delCommentLik(String commentLikeId) {
        return userActivityMapper.delCommentLik(commentLikeId);
    }

    @Override
    public Integer delNoteCommentLik(String likeId) {
        return userActivityMapper.delNoteCommentLik(likeId);
    }

    @Override
    public Integer addCommentLik(String commentId, String userId) {
        return userActivityMapper.addCommentLik(commentId, userId);
    }

    @Override
    public Integer addNoteCommentLik(String commentId, String userId) {
        return userActivityMapper.addNoteCommentLik(commentId, userId);
    }

    @Override
    public Integer getCommentLikNum(String commentId) {
        return userActivityMapper.getCommentLikNum(commentId);
    }

    @Override
    public Integer getNoteCommentLikNum(String commentId) {
        return userActivityMapper.getNoteCommentLikNum(commentId);
    }

    @Override
    public List<ActivityCommentAndReplyVo> getCommentAndReply(String activityId, String userId) {
        return userActivityMapper.getCommentAndReply(activityId, userId);
    }

    @Override
    public List<NoteCommentAndReplyVo> getNoteCommentAndReply(String noteId, String userId) {
        return userActivityMapper.getNoteCommentAndReply(noteId, userId);
    }

    @Override
    public Integer saveOrUpdateLikeNum(String likeNum, String commentId) {
        if (userActivityMapper.selectCommentLikNum(commentId) == null || userActivityMapper.getCommentLikNum(commentId) == null) {
            userActivityMapper.saveCommentLikNum(commentId, likeNum);
            return null;
        }
        userActivityMapper.updateCommentLikNum(commentId, likeNum);
        return null;
    }

    @Override
    public Integer updateNoteCommentLikeNum(String likeNum, String commentId) {
        return userActivityMapper.updateNoteCommentLikeNum(commentId, likeNum);
    }

    @Override
    public List<Map<String, String>> getUserActivityLikeMap(String activityId, String userId) {
        return userActivityMapper.getUserActivityLikeMap(activityId, userId);
    }

    @Override
    public List<Map<String, String>> getUserNoteLikeMap(String noteId, String userId) {
        return userActivityMapper.getUserNoteLikeMap(noteId, userId);
    }

    @Override
    public List<Map<String, String>> getUserActivityCollectMap(String activityId, String userId) {
        return userActivityMapper.getUserActivityCollectMap(activityId, userId);
    }

    @Override
    public List<Map<String, String>> getUserNoteCollectMap(String noteId, String userId) {
        return userActivityMapper.getUserNoteCollectMap(noteId, userId);
    }

    @Override
    public void delUserActivityLikeMap(String activityId, String userId) {
        userActivityMapper.delUserActivityLikeMap(activityId, userId);
    }

    @Override
    public void delUserNoteLikeMap(String noteId, String userId) {
        userActivityMapper.delUserNoteLikeMap(noteId, userId);
    }

    @Override
    public void delUserActivityCollectMap(String activityId, String userId) {
        userActivityMapper.delUserActivityCollectMap(activityId, userId);
    }

    @Override
    public void delUserNoteCollectMap(String noteId, String userId) {
        userActivityMapper.delUserNoteCollectMap(noteId, userId);
    }

    @Override
    public void addUserActivityLikeMap(String activityId, String userId) {
        userActivityMapper.addUserActivityLikeMap(activityId, userId);
    }

    @Override
    public void addUserNoteLikeMap(String noteId, String userId) {
        userActivityMapper.addUserNoteLikeMap(noteId, userId);
    }

    @Override
    public void addUserActivityCollectMap(String activityId, String userId) {
        userActivityMapper.addUserActivityCollectMap(activityId, userId);
    }

    @Override
    public void addUserNoteCollectMap(String noteId, String userId) {
        userActivityMapper.addUserNoteCollectMap(noteId, userId);
    }

    @Override
    public List<Map<String, String>> getUserActivityParticipant(Integer activityId, Integer userId) {
        return userActivityMapper.getUserActivityParticipant(activityId, userId);
    }

    @Override
    public Integer getIdentityVerification(String userId) {
        return userActivityMapper.getIdentityVerification(userId);
    }

    @Override
    public List<Map<String, String>> getPhotoWallLike(String wallId, String userId) {
        return userActivityMapper.getPhotoWallLike(wallId, userId);
    }

    @Override
    public void addPhotoWallLike(String wallId, String userId) {
        userActivityMapper.addPhotoWallLike(wallId, userId);
    }

    @Override
    public void delPotoWallLike(String wallId, String userId) {
        userActivityMapper.delPhotoWallLike(wallId, userId);
    }

    @Override
    public void updateIdentityVrification(String userId) {
        userActivityMapper.updateIdentityVrification(userId);
    }

}
